//
//  RTJBidRecordsDetailsController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/10.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJBidRecordsDetailsController.h"
#import "RTJBidRecordsDetailsCell.h"
#import "RTJBidRecordsDetailsCell.h"
#import "RTJTheContractController.h"
#import "RTJAutoBidDetailsController.h"
#import "RTJCancelAutoMarkView.h"
#import "LewPopupViewAnimationFade.h"
#import "RTJReceivableController.h"

@interface RTJBidRecordsDetailsController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSDictionary *data;
@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,assign) BOOL isCancel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom;
@property (weak, nonatomic) IBOutlet UILabel *tips;

@end

@implementation RTJBidRecordsDetailsController

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.estimatedRowHeight=300;
    
    self.tips.text = _isAuto ? @"说明：“预期收益”显示的金额为“已匹配金额”的标的预期收益金额，收益以实际投资收益为准！网贷有风险，出借需谨慎！" :@"";
    self.bottom.constant = _isAuto ? 20 :0;
    
    [self loadData];
}

-(void)chexiao:(UIButton *)btn{
    
    RTJCancelAutoMarkView *view = [RTJCancelAutoMarkView defaultViewWithFrame:CGRectMake(0, 0, 325, 265) title:self.title];
    view.parentVC = self;
    [self lew_presentPopupView:view animation:[LewPopupViewAnimationFade new] dismissed:^{
        
        NSLog(@"动画结束");
        
    }];
    
    [view withSureClick:^(NSString * _Nonnull pswStr) {
        
        [SVProgressHUD showWithStatus:nil];

        //验证支付密码是否正确
        [RTJNetWork asyncRequestWithURL:@"user/check_pay_password" params:@{@"pay_password":pswStr} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
            
            if (success) {
                
                if ([data[@"status"]integerValue]==1) {
                    
                    //撤销投标
                    [self cancelBin:pswStr];
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"密码错误:%@",data[@"msg"]]];
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"密码错误:%@",errorMsg]];
                
            }
            
        }];
        
    }];

}

-(void)cancelBin:(NSString *)psw{
    
    [RTJNetWork asyncRequestWithURL:@"user/cancel_join_plan" params:@{@"id":_binId,@"paypassword":psw} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                [self.btn setTitle:@"已撤销" forState:UIControlStateNormal];
                self.btn.userInteractionEnabled = NO;
                self.isCancel = YES;
                [self.tableView reloadData];
                
                if (self.delegate &&[self.delegate respondsToSelector:@selector(sureCancel:)] ) {
                    
                    [self.delegate sureCancel:YES];
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            
        }
        
    }];
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    [self.tableView showEmptyViewWithStr:@"" image:nil callback:nil];
    
    [RTJNetWork asyncRequestWithURL:self.isAuto ? @"user/lend_auto": @"user/lend_detail" params:@{@"id":_binId} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [self.tableView removeEmptyView];
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.data = data;
                [self.tableView reloadData];
                
                if (self.isAuto) {
                
                    UIButton *clear=[UIButton buttonWithType:UIButtonTypeSystem];
                    clear.size=CGSizeMake(60, 30);
                    [clear setTitleColor:[UIColor colorForHex:@"#666666"] forState:UIControlStateNormal];
                    clear.titleLabel.font=[UIFont systemFontOfSize:14];
                    self.btn = clear;
                    [clear addTarget:self action:@selector(chexiao:) forControlEvents:UIControlEventTouchUpInside];
                    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:self.btn];
                    
                    NSInteger match_deal = [data[@"data"][@"match_deal"]integerValue];
                    NSInteger status = [data[@"data"][@"status"]integerValue];
                    
                    if (status == -1) {
                        
                        [self.btn setTitle:@"已撤销" forState:UIControlStateNormal];
                        self.btn.userInteractionEnabled = NO;
                        
                        
                    }else if (match_deal == 0 && status == 0){
                        
                        [self.btn setTitle:@"撤销" forState:UIControlStateNormal];
                        self.btn.userInteractionEnabled = YES;
                    }
                    else{
                        
                        self.btn.hidden = YES;
                        
                    }
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                [self.tableView showEmptyViewWithStr:data[@"msg"] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            [self.tableView showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                
                [self loadData];
            }];

        }
        
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.data!=nil ? 1: 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJBidRecordsDetailsCell";
    
    RTJBidRecordsDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
            
        cell = [[NSBundle mainBundle] loadNibNamed:@"RTJBidRecordsDetailsCell" owner:self options:nil][0];
    }
    
    cell.isAuto = self.isAuto;
    cell.isCancel = self.isCancel;
    cell.data = self.data;
    [cell.hetong addTarget:self action:@selector(hetong) forControlEvents:UIControlEventTouchUpInside];
    [cell.huikuanBtn addTarget:self action:@selector(huikuan) forControlEvents:UIControlEventTouchUpInside];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

-(void)huikuan{
    
    RTJReceivableController *vc = [[RTJReceivableController alloc]init];
    vc.binId = _binId;
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)hetong{
    
    if (self.isAuto) {
        
        RTJAutoBidDetailsController *vc = [[RTJAutoBidDetailsController alloc]init];
        vc.title = @"投标详情";
        vc.idStr = [NSString stringWithFormat:@"%@",self.data[@"data"][@"id"]];
        [self.navigationController pushViewController:vc animated:YES];

    }else{
        
        RTJTheContractController *vc = [[RTJTheContractController alloc]init];
        vc.contractStr = [NSString stringWithFormat:@"%@",_data[@"data"][@"contract_no"]];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
   
    
}

@end
